//
//  StrategyDetailTableViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StrategyDetailTableViewController.h"
#import "DestinationTableViewController.h"
#import "StrategyDetailTableViewCell.h"
#import "OfflineTableViewController.h"
#import "RouteTableViewController.h"
#import "StrTableViewController.h"
#import "SpeTableViewController.h"
#import "UIImageView+WebCache.h"
#import "strategyModel.h"
#import "CDHttpHelper.h"
#import "FMDBHelpr.h"
@interface StrategyDetailTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray * allDataArray;

@end
#define kRandom arc4random()%255/256.0
#define url @"https://chanyouji.com/api/destinations/%ld.json?page=1"
@implementation StrategyDetailTableViewController

- (void)viewDidLoad {
    
    
    /*
     
     
     攻略详情界面
     
     */
    
    
    
    [super viewDidLoad];
    self.allDataArray = [NSMutableArray array];
    
    self.navigationItem.title = [self.strModel.name_zh_cn stringByAppendingString:@"攻略"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"StrategyDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"strategyDetail"];
    
    self.tableView.bounces = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self handleData];
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/destinations/%ld.json?page=1",self.strModel.id] params:nil success:^(id responseObj) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dict in array) {
            strategyModel *strModel = [strategyModel new];
            [strModel setValuesForKeysWithDictionary:dict];
            [self.allDataArray addObject:strModel];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrategyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"strategyDetail" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StrategyDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"strategyDetail"];
    }
    strategyModel *strModel = self.allDataArray[indexPath.row];
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:strModel.image_url]];
    cell.titleLabel.font = [UIFont systemFontOfSize:20];
//    cell.titleLabel.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.6];
    cell.titleLabel.textColor = [UIColor orangeColor];
    NSString *str = [strModel.name_zh_cn stringByAppendingString:[NSString stringWithFormat:@" %@",strModel.name_en]];
    
    cell.titleLabel.text = str;
    [cell.strategyButton addTarget:self action:@selector(pushToStrategy:) forControlEvents:UIControlEventTouchUpInside];
    [cell.routeButton addTarget:self action:@selector(pushToRoute:) forControlEvents:UIControlEventTouchUpInside];
    [cell.destinationButton addTarget:self action:@selector(pushToDestinatioin:) forControlEvents:UIControlEventTouchUpInside];
    [cell.specialButton addTarget:self action:@selector(pushToSpecial:) forControlEvents:UIControlEventTouchUpInside];
    cell.downloadButton.layer.cornerRadius = 8;
    cell.downloadButton.layer.masksToBounds = YES;
    [cell.downloadButton addTarget:self action:@selector(downloadAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.downloadButton.tag = indexPath.row;
    cell.strategyButton.tag = indexPath.row;
    cell.routeButton.tag = indexPath.row;
    cell.destinationButton.tag = indexPath.row;
    cell.specialButton.tag = indexPath.row;
    return cell;
}

- (void)downloadAction:(UIButton *)sender{
    
    strategyModel *model = self.allDataArray[sender.tag];
//    [[FMDBHelpr shareSQLite] createTable];
//    [[FMDBHelpr shareSQLite] insertDataWithUser:model];
    model.lat = self.strModel.lat;
    model.lng = self.strModel.lng;
    OfflineTableViewController *offVC = [[OfflineTableViewController alloc]init];
    offVC.dataArray = [NSMutableArray array];
    [offVC.dataArray addObject:model];
    NSLog(@"%ld",offVC.dataArray.count);
    [self.navigationController pushViewController:offVC animated:YES];
    
}

- (void)pushToStrategy:(UIButton *)sender{
    StrTableViewController *strVC = [[StrTableViewController alloc]init];
    strategyModel *model = self.allDataArray[sender.tag];
    strVC.ID = model.id;
    [self.navigationController pushViewController:strVC animated:YES];
}

- (void)pushToRoute:(UIButton *)sender{
    RouteTableViewController *routeVC = [[RouteTableViewController alloc]init];
    strategyModel *model = self.allDataArray[sender.tag];
    routeVC.ID = model.id;
    routeVC.name = model.name_zh_cn;
    [self.navigationController pushViewController:routeVC animated:YES];
}

- (void)pushToDestinatioin:(UIButton *)sender{
    
    DestinationTableViewController *desVC = [[DestinationTableViewController alloc]init];
    strategyModel *model = self.allDataArray[sender.tag];
    desVC.strategyModel = model;
    desVC.strategyModel.lat = self.strModel.lat;
    desVC.strategyModel.lng = self.strModel.lng;
    for (strategyModel *strModel in self.allDataArray) {
        [desVC.numArray addObject:strModel.name_zh_cn];
    }
    strategyModel *model1 = self.allDataArray[0];
    desVC.content_count = model1.contents_count;
    [self.navigationController pushViewController:desVC animated:YES];
    
}

- (void)pushToSpecial:(UIButton *)sender{
    
    SpeTableViewController *speVC = [[SpeTableViewController alloc]init];
    strategyModel *model = self.allDataArray[sender.tag];
    speVC.id = model.id;
    
    [self.navigationController pushViewController:speVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [UIScreen mainScreen].bounds.size.height / 2;
}


@end
